package SMS.controller;

import SMS.model.Res;
import SMS.pojo.Students;
import SMS.service.StudentService;
import SMS.vo.IId;
import SMS.vo.IStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王天宇
 */
@Controller
@CrossOrigin
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService; // 假设你有一个学生服务来处理业务逻辑


//    查询学生信息列表
    @GetMapping("/list")
    @ResponseBody
    public Res<List<Object>> list(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 查询学生信息，假设 studentService 返回一个学生列表
        List<IStudent> students = studentService.getStudents(currentPage, pageSize);
        int totalCount = studentService.getTotalCount();
        // 封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("students", students);
        data.put("totalCount", totalCount);

        Res<List<Object>> res = new Res(200, "success", data);
//        System.out.println(res);
        return res;
    }

//    更新学生信息
    @PostMapping ("/update")
    @ResponseBody
    public Res update(@RequestBody Students student) {
//        System.out.println("controller:"+student);
        // 更新学生信息，假设 studentService 处理更新逻辑
        studentService.updateStudent(student);
        return new Res(200, "success", null);
    }

//    删除学生数据
    @PostMapping("/delete")
    @ResponseBody
    public Res delete(@RequestBody IId req) {
        // 删除学生信息，假设 studentService 处理删除逻辑
        studentService.deleteStudent(req.id);
        return new Res(200, "success", null);
    }
//    添加学生数据
    @PostMapping("/add")
    @ResponseBody
    public Res add(@RequestBody Students student) {
        // 添加学生信息，假设 studentService 处理添加逻辑
        studentService.addStudent(student);
        return new Res(200, "success", null);
    }

//    按学号或姓名查询学生数据
    @GetMapping ("/search")
    @ResponseBody
    public Res search(@RequestParam(value = "keyword") String req,
                      @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                      @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        // 假设req是学生的学号，按学号查询学生数量
        int totalCount = studentService.countByStuNo(req);
        // 按学号查询不到任何学生
        if (totalCount == 0){
            totalCount = studentService.countByStuName(req);
            // 按姓名查询不到任何学生
            if (totalCount == 0){
                return new Res(400, "学号或姓名错误或该学生不存在", null);
            }
            // 按姓名查询到学生
            else {
                // 查询学生信息，假设 studentService 返回一个学生列表
                List<IStudent> students = studentService.searchStudentByName(req, currentPage, pageSize);
                // 封装数据
                Map<String, Object> data = new HashMap<>();
                data.put("students", students);
                data.put("totalCount", totalCount);

                return new Res(200, "success", data);
            }
        }
        // 按学号查询到学生
        else {
            // 查询学生信息，假设 studentService 返回一个学生列表
            List<IStudent> students = studentService.searchStudentByNo(req,currentPage, pageSize);
            // 封装数据
            Map<String, Object> data = new HashMap<>();
            data.put("students", students);
            data.put("totalCount", totalCount);
            return new Res(200, "success", data);
        }
    }
}
